import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})

export class Clone {
  
  constructor() { }
  deepClone <T>(target: T, map: any = new WeakMap()):T {
    if(typeof target === 'object') {
      let cloneTarget: Partial<T> = Array.isArray(target) ? [] as any : {}
      if(map.get(target)) {
        return map.get(target)
      }
      map.set(target, cloneTarget)
      for (const key in target) {
        cloneTarget[key] = target[key]
      }
      return cloneTarget as T
    } else {
      return target
    }
  }
}

